home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9275 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.gate.net!!bcfreenet!z007400b
  2. From: z007400b@bcfreenet.seflin.lib.fl.us (Ralph Silverman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Binary files ?
  5. Date: 7 Mar 1996 19:40:51 GMT
  6. Organization: SEFLIN Free-Net - Broward
  7. Message-ID: <4hne43$ihi@news.seflin.lib.fl.us>
  8. References: <4gheee$bvr@ci.ist.utl.pt>  <Pine.SV4.3.91-heb-2.04.960224055809.17461A-100000@cs.technion.ac.il>
  9. NNTP-Posting-Host: ns2.seflin.lib.fl.us
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Kohn Emil Dan (emild@cs.technion.ac.il) wrote:
  13.  
  14.  
  15. : On 22 Feb 1996, delphis wrote:
  16.  
  17. : > 
  18. : > 
  19. : > 
  20. : > 
  21. : >     Greetings from a frustrated c programmer!
  22. : >     I program in c a couple of years but i never figure out this basic 
  23. : > question : how to work with binary files ? basically, what i want to do 
  24. : > is writing a matrix to a file and read it back but when i try the 
  25. : > following code in dos the matrix comes all gobled, but works fine in unix!
  26. : >     Can anyone tell me whats wrong with this ? I have been using text files
  27. : > and they dont give any troble at all!
  28. : > 
  29. : > Obrigadissimo!
  30. : > 
  31. : > 
  32. : > 
  33. : > 
  34. : > # include <stdio.h>
  35. : > 
  36. : > 
  37. : > void main()
  38. : > {
  39. : > 
  40. : > /*write file*/
  41. : > int n;
  42. : > unsigned char mat[200]; 
  43. : > FILE *f=fopen("teste","w"); /*do i have to use the CREATE before this ?*/
  44. : > for (n=0;n<200;n++) mat[n]=n; /*init matrix*/
  45. : > fwrite(mat,200,1,f);
  46. : > fclose(f);
  47. : > 
  48. : > /*reset matrix*/
  49. : > for (n=0;n<200;n++) mat[n]=0;
  50. : > 
  51. : > /*read file*/
  52. : > f=fopen("teste","r");
  53. : > fread(mat,200,1,f);
  54. : > fclose(f);
  55. : > for (n=0;n<200;n++) printf("%d ",mat[n]);/*print it*/
  56. : > }
  57. : > 
  58. : > 
  59.  
  60. : Just open the file using  "rb" for reading and "wb" for writing, (instead 
  61. : of "r", and "w" respectively, and everything will be ok. 
  62.  
  63. : In UNIX, there is no difference between text and binary files, a file is 
  64. : a stream of bytes. In DOS, the situation is much stranger, I think he 
  65. : expands \n to \n\r, and puts an EOF (CTRL-Z) at the end of file, when a 
  66. : file is opened in text mode.
  67.  
  68. : And last, but not least: main() returns int, not void...
  69.  
  70.  
  71. : Hope this helps.
  72.  
  73. :                         Regards, 
  74.  
  75. :                             Emil
  76.  
  77. --
  78. *********begin r.s. response*************
  79.  
  80.  
  81.     prior response is accurate...
  82.     the answer is to go to
  83.         low level i.o.
  84.         ^^^^^^^^^^^^^^
  85.     .
  86.  
  87. *********end r.s. response***************
  88. Ralph Silverman
  89. z007400b@bcfreenet.seflin.lib.fl.us
  90.  
  91.